package com.crm.web.controller.business;

import java.util.List;

import com.crm.common.utils.ShiroUtils;
import com.crm.system.domain.BiePatient;
import com.crm.system.service.IBiePatientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.crm.common.annotation.Log;
import com.crm.common.enums.BusinessType;
import com.crm.system.domain.BieMedication;
import com.crm.system.service.IBieMedicationService;
import com.crm.common.core.controller.BaseController;
import com.crm.common.core.domain.AjaxResult;
import com.crm.common.utils.poi.ExcelUtil;
import com.crm.common.core.page.TableDataInfo;

/**
 * 用药情况Controller
 * 
 * @author crm
 * @date 2021-05-10
 */
@Controller
@RequestMapping("/system/medication")
public class BieMedicationController extends BaseController
{
    private String prefix = "system/medication";

    @Autowired
    private IBieMedicationService bieMedicationService;

    @Autowired
    private IBiePatientService biePatientService;

    @RequiresPermissions("system:medication:view")
    @GetMapping()
    public String medication()
    {
        return prefix + "/medication";
    }

    /**
     * 查询用药情况列表
     */
    @RequiresPermissions("system:medication:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BieMedication bieMedication)
    {
        startPage();
        List<BieMedication> list = bieMedicationService.selectBieMedicationList(bieMedication);
        return getDataTable(list);
    }

    /**
     * 导出用药情况列表
     */
    @RequiresPermissions("system:medication:export")
    @Log(title = "用药情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BieMedication bieMedication)
    {
        List<BieMedication> list = bieMedicationService.selectBieMedicationList(bieMedication);
        ExcelUtil<BieMedication> util = new ExcelUtil<BieMedication>(BieMedication.class);
        return util.exportExcel(list, "用药情况数据");
    }

    /**
     * 新增用药情况
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用药情况
     */
    @RequiresPermissions("system:medication:add")
    @Log(title = "用药情况", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BieMedication bieMedication)
    {

        //添加患者编号
        Long userId = ShiroUtils.getUserId();
        BiePatient biePatient = new BiePatient();
        biePatient.setUserId(userId);
        System.out.println(userId);
        List<BiePatient> biePatients = biePatientService.selectBiePatientList(biePatient);
        String patientId = biePatients.get(0).getPatientId();
        bieMedication.setPatientId(patientId);
        return toAjax(bieMedicationService.insertBieMedication(bieMedication));
    }

    /**
     * 修改用药情况
     */
    @GetMapping("/edit/{medicationId}")
    public String edit(@PathVariable("medicationId") String medicationId, ModelMap mmap)
    {
        BieMedication bieMedication = bieMedicationService.selectBieMedicationById(medicationId);
        mmap.put("bieMedication", bieMedication);
        return prefix + "/edit";
    }

    /**
     * 修改保存用药情况
     */
    @RequiresPermissions("system:medication:edit")
    @Log(title = "用药情况", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BieMedication bieMedication)
    {
        return toAjax(bieMedicationService.updateBieMedication(bieMedication));
    }

    /**
     * 删除用药情况
     */
    @RequiresPermissions("system:medication:remove")
    @Log(title = "用药情况", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bieMedicationService.deleteBieMedicationByIds(ids));
    }
}
